#include <iostream>
#include <string>
using namespace std;

int main()
{
    int n;
    cin >> n;
    while(n--)
    {
        int len, k;
        cin >> len >> k;
        string str;
        cin >> str;
        int sum = 0, ret = 0;
        bool flag = false;
        for(auto e : str)
        {
            if(e == 'W')
            {
                sum++;
                if(sum >= 3)
                    ret += k;
                else ret++;
            }
            else 
            {
                sum = 0;
                ret--;
            }
        }
        cout << ret << endl;
    }
    return 0;
}
